/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobilevikings.app.topuphistory;

import com.mobilevikings.app.Function;
import com.mobilevikings.app.LoadAlert;
import com.mobilevikings.app.api.InvalidResponseException;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import org.netbeans.microedition.lcdui.SimpleTableModel;
import org.netbeans.microedition.lcdui.TableItem;
import org.netbeans.microedition.lcdui.TableModel;

/**
 *
 * @author Jeroen
 */
public class TopUpHistoryFunction extends Function implements Runnable {

    private Form topUpHistoryForm;
    private Vector history;
    private final Alert mAlrtRetrievingFailed;
    private final LoadAlert mAlrtRetrieving;
    private TableItem topUpTable;
    private TableModel topUpTableModel;

    public TopUpHistoryFunction(Display display) {
        super(display);
        history = null;
        mAlrtRetrieving = new LoadAlert(mProperties.get("TOPUP_HISTORY"), mProperties.get("TOPUP_HISTORY_BUSY"), this);
        mAlrtRetrievingFailed = new Alert(mProperties.get("TOPUP_HISTORYE_GET_FAILED"), "", null, AlertType.ERROR);
    }

    public void show() {
        mDisplay.setCurrent(mAlrtRetrieving);
        // mCancelled = false;

        Thread t = new Thread(this);
        t.start();
    }

    public void commandAction(Command c, Displayable d) {
        if (c == mCmdPrevious) {
            mDisplay.setCurrent(mPreviousScreen);
        }
    }

    public void run() {
        if (history == null) {
            try {
                history = mNetlogConnection.getTopUpHistory();
            } catch (InvalidResponseException ex) {
                mAlrtRetrievingFailed.setString(mProperties.get("ERROR_INVALIDRESPONSE") + ":\n" + ex.getDetails());
            } catch (IOException ex) {
                mAlrtRetrievingFailed.setString(mProperties.get("ERROR_IO"));
            }
        }

        if (history == null) {
            mDisplay.setCurrent(mAlrtRetrievingFailed, mPreviousScreen);
        } else {
            initTopupHistoryForm();
            mDisplay.setCurrent(topUpHistoryForm);
        }

    }

    private void initTopupHistoryForm() {
        topUpHistoryForm = new Form(mProperties.get("TOPUP_HISTORY"));

        // Set up the table
        topUpTable = new TableItem(mDisplay, "");
        topUpTableModel = new SimpleTableModel(getTopupValues(history), getColumnValues());
        topUpTable.setModel(topUpTableModel);

        // Add the table to the form
        topUpHistoryForm.append(topUpTable);

        topUpHistoryForm.setCommandListener(this);
        topUpHistoryForm.addCommand(mCmdPrevious);
    }

    private String[][] getTopupValues(Vector history) {
        String[][] values = new String[history.size()][3];
        TopUp topup;
        for (int i = 0; i < history.size(); i++) {
            topup = (TopUp) history.elementAt(i);
            values[i][0] = Double.toString(topup.getAmount());
            values[i][1] = topup.getDate();
            values[i][2] = topup.getMethod();
        }
        return values;
    }

    private String[] getColumnValues() {
        return new String[]{mProperties.get("TOPUP_AMOUNT"), mProperties.get("DATE"), mProperties.get("METHOD")};
    }
}
